<script setup lang="ts">
import GaNavigationBar from "@/components/GaNavigationBar.vue";
import {
    GaRequestConfig,
    GaRequestMethod,
    GaRequestBodyInputType, GaResponseBodyOutputType, GaRequestManager, GaNetworkStatusCode,
    TwResultModel,
    GaMd5Manager
} from 'ga_utility_sdk_base';
import { UserModel } from "@/model/UserModel"
import type LoginInfo from "@/model/UserModel"
import { GlobalTool } from "@/utils/GlobalTool"
import router from "@/router"
function onLoginByPassword() {
    let account: string = "test";
    let password: string = "910509";
    let encryptPassword = GaMd5Manager.md5String32lower(password);
    let _loginInfo: LoginInfo = {
        account: account,
        password: encryptPassword
    }
    let config = new GaRequestConfig(GaRequestMethod.POST, GaRequestBodyInputType.OBJ_2_JSON, GaResponseBodyOutputType.BLOB_2_JSONOBJ);
    config.requestBodyInput = _loginInfo;
    config.apiPath = "User/Login";
    GaRequestManager.sharedInstance().invokeRequest<UserModel>(config, (status: GaNetworkStatusCode, res: TwResultModel<UserModel>, msg, info) => {
        if (status == GaNetworkStatusCode.Ok) {
            let code = res.code;
            if (code == 0) {
                //响应成功.
                console.log(res);
                let user: UserModel = res.resultData.item as UserModel;
                GlobalTool.setCurrentUser(user);
                console.log(`login success,token is:${user.token}`)
                GaRequestManager.sharedInstance().setCommonHeaderPair("Authorization", user.token);
                return;
            }
        }
        //响应失败.
        console.log(`status is: ${status},msg is: ${msg}`)
    });
}
function onLoginByToken() {
    let user:UserModel = GlobalTool.getCurrentUser();
    let userId = user.id;
    if(!userId) return;
    let tokenLoginInfo = {
        id:userId
    }
    let config = new GaRequestConfig(GaRequestMethod.POST, GaRequestBodyInputType.OBJ_2_JSON, GaResponseBodyOutputType.BLOB_2_JSONOBJ);
    config.requestBodyInput = tokenLoginInfo;
    config.apiPath = "User/Login/Token";
    GaRequestManager.sharedInstance().invokeRequest<UserModel>(config, (status: GaNetworkStatusCode, res: TwResultModel<UserModel>, msg, info) => {
        console.log(`info is ${info}`);
        if (status == GaNetworkStatusCode.Ok) {
            let code = res.code;
            if (code == 0) {
                //响应成功.
                console.log(res);
                let user: UserModel = res.resultData.item as UserModel;
                GlobalTool.setCurrentUser(user);
                console.log(`login success,token is:${user.token}`)
                GaRequestManager.sharedInstance().setCommonHeaderPair("Authorization", user.token);
                return;
            }
        }
        // //响应失败.
        // console.log(`status is: ${status},msg is: ${msg}`)
    });
}
function onLogout() {
    let user:UserModel = GlobalTool.getCurrentUser();
    let userId = user.id;
    if(!userId) return;
    let info = {
        id:userId
    }
    let config = new GaRequestConfig(GaRequestMethod.POST, GaRequestBodyInputType.OBJ_2_JSON, GaResponseBodyOutputType.BLOB_2_JSONOBJ);
    config.requestBodyInput = info;
    config.apiPath = "User/Logout";
    GaRequestManager.sharedInstance().invokeRequest<UserModel>(config, (status: GaNetworkStatusCode, res: TwResultModel<UserModel>, msg, info) => {
        console.log(`info is ${info}`);
        if (status == GaNetworkStatusCode.Ok) {
            let code = res.code;
            if (code == 0) {
                //响应成功.
                return;
            }
        }
        //响应失败.
        console.log(`status is: ${status},msg is: ${msg}`)
    });
}
// function onLoginByPassword() {
//     let account: string = "test";
//     let password: string = "910509";
//     let encryptPassword = GaMd5Manager.md5String32lower(password);
//     let _loginInfo: LoginInfo = {
//         account: account,
//         password: encryptPassword
//     }
//     let config = new GaRequestConfig(GaRequestMethod.POST, GaRequestBodyInputType.OBJ_2_JSON, GaResponseBodyOutputType.BLOB_2_JSONOBJ);
//     config.requestBodyInput = _loginInfo;
//     config.apiPath = "User/Login";
//     GaRequestManager.sharedInstance().invokeRequest<UserModel>(config, (status: GaNetworkStatusCode, res: TwResultModel<UserModel>, msg, info) => {
//         if (status == GaNetworkStatusCode.Ok) {
//             let code = res.code;
//             if (code == 0) {
//                 //响应成功.
//                 console.log(res);
//                 let user: UserModel = res.resultData.item as UserModel;
//                 GlobalTool.setCurrentUser(user);
//                 console.log(`login success,token is:${user.token}`)
//                 GaRequestManager.sharedInstance().setCommonHeaderPair("Authorization", user.token);
//                 return;
//             }
//         }
//         //响应失败.
//         console.log(`status is: ${status},msg is: ${msg}`)
//     });
// }
</script>

<template>
    <div id="container">
        <div id="content">
            <GaNavigationBar title="Category"></GaNavigationBar>
            <el-row>
                <el-button @click="onLoginByPassword">LoginByPassword</el-button>
            </el-row>
            <el-row>
                <el-button @click="onLoginByToken">LoginByToken</el-button>
            </el-row>
            <el-row>
                <el-button @click="onLogout">Logout</el-button>
            </el-row>
            <el-row>
                <!-- <el-button @click="onRegister">Register</el-button> -->
            </el-row>
        </div>
    </div>
</template>

<style scoped>
#container {
    display: flex;
    align-items: center;
    /* background-color: gray; */
    justify-content: center;
}
</style>